package cn.cqautotest.cqviehelper.utils;

import android.annotation.SuppressLint;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ArrayUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.zhuangfei.timetable.model.ScheduleSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressLint("SimpleDateFormat")
public class MyScheduleSupport extends ScheduleSupport {

    private static final String TAG = MyScheduleSupport.class.getSimpleName();
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final String[] slideBarTimes = new String[]{
            "08:30\n09:15",
            "09:20\n10:05",
            "10:25\n11:10",
            "11:15\n12:00",
            "14:00\n14:45",
            "14:50\n15:35",
            "15:45\n16:30",
            "16:35\n17:20",
            "19:00\n19:45",
            "19:50\n20:35"
    };

    /**
     * 获取几周之前的  时间戳
     *
     * @param milliseconds 转换前的时间戳
     * @param week         几周前
     * @return 转换后的时间戳
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long getAFewWeeksBeforeTimestamp(long milliseconds, int week) {
        LocalDateTime localDateTime = getLocalDateTime(milliseconds).minusWeeks(week - 1);
        return convertLocalDateTimeToTimestamp(localDateTime);
    }

    /**
     * 获取几周之后的时间戳
     *
     * @param milliseconds 转换前的时间戳
     * @param week         几周后
     * @return 转换后的时间戳
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long getAFewWeeksLaterTimestamp(long milliseconds, int week) {
        LocalDateTime localDateTime = getLocalDateTime(milliseconds).plusWeeks(week - 1);
        return convertLocalDateTimeToTimestamp(localDateTime);
    }

    /**
     * 将 LocalDateTime 对象转换成 时间戳
     *
     * @param localDateTime 将要转换的LocalDateTime对象
     * @return timestamp 13位的时间戳
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long convertLocalDateTimeToTimestamp(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 根据时间戳获取 LocalDateTime 对象
     *
     * @param milliseconds 时间戳
     * @return LocalDateTime 对象
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static LocalDateTime getLocalDateTime(long milliseconds) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault());
    }

    /**
     * 根据开学时间计算当前周
     *
     * @param startTime 满足"yyyy-MM-dd HH:mm:ss"模式的字符串
     * @return 周次
     */
    public static int timeTransform(String startTime) {
        try {
            long start = sdf.parse(startTime).getTime();
            long end = new Date().getTime();
            long seconds = (end - start) / 1000;
            long day = seconds / (24 * 3600);
            int week = (int) (Math.floor(day / 7) + 1);
            return week;
        } catch (ParseException e) {
            return -1;
        }
    }

    /**
     * 根据开学时间计算当前周
     *
     * @param startTimestamp 长度为13位的时间戳
     * @return 周次
     */
    public static int timeTransform(long startTimestamp) {
        String format = sdf.format(startTimestamp);
        return timeTransform(format);
    }

    /**
     * 获取到默认的侧边栏时间数组
     *
     * @return slideBarTimes 侧边栏的时间数组
     */
    public static String[] getDefaultSlideTimes() {
        return slideBarTimes;
    }

    /**
     * 获取侧边栏的时间集合
     *
     * @return List<String> 侧边栏的时间集合
     */
    public static List<String> getSlidBarTimeList() {
        return ArrayUtils.asArrayList(slideBarTimes);
    }

    /**
     * 根据节次获取侧边栏的时间
     *
     * @param festival 节次
     * @return slideBarTime 侧边栏的时间
     */
    public static String getSlideTimeByFestival(int festival) {
        return getSlidBarTimeList().get(festival - 1);
    }

    /**
     * 获取节次区间时间
     *
     * @param startFestival 开始的节次
     * @param endFestival   结束的节次
     * @return 节次区间时间
     */
    public static String[] getFestivalIntervalTime(int startFestival, int endFestival) {
        String timeByStartFestival = getSlideTimeByFestival(startFestival);
        String timeByEndFestival = getSlideTimeByFestival(endFestival);
        String join = StringJoinUtils.join(timeByStartFestival, "\n", timeByEndFestival);
        String[] split = join.split("\n");
        String[] festivalIntervalTime = new String[2];
        festivalIntervalTime[0] = split[0];
        festivalIntervalTime[1] = split[split.length - 1];
        return festivalIntervalTime;
    }
}
